//
//  LeftCityView.m
//  zjGovernment
//
//  Created by 胡胜冬 on 2017/2/18.
//  Copyright © 2017年 胡胜冬. All rights reserved.
//

#import "LeftCityView.h"
#import "CityAddressCell.h"

@interface LeftCityView ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) UIView *rightLineView;
@property(nonatomic, strong) NSIndexPath *indexPath;

@end

@implementation LeftCityView

- (void)setCityDataArray:(NSArray *)cityDataArray {
    _cityDataArray = cityDataArray;
    if (!_cityDataArray) {
        _cityDataArray = [NSArray array];
    }
    [self.tableView reloadData];
}

- (void)setIndexPath:(NSIndexPath *)indexPath {
    _indexPath = indexPath;
    [self.tableView reloadData];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        _cityDataArray = [NSArray array];
        
        [self initTableView];
        [self initLineView];
    }
    return self;
}

- (void)initTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.bounds style: UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"ffffff"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"CityAddressCell" bundle:nil] forCellReuseIdentifier:@"CityAddressCell"];
    
    [self addSubview:self.tableView];
}

- (void)initLineView {
    self.rightLineView = [[UIView alloc]initWithFrame:CGRectMake(self.bounds.size.width - 0.5, 0, 0.5, self.bounds.size.height)];
    self.rightLineView.backgroundColor = [UIColor colorWithHexString:@"e7e7e7"];
    [self addSubview:self.rightLineView];
    [self bringSubviewToFront:self.rightLineView];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cityDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CityAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CityAddressCell" forIndexPath:indexPath];
    
    NSDictionary *dataDic = self.cityDataArray[indexPath.row];
    
    
    cell.titleString = dataDic[@"text"];
    
    if (self.indexPath && (indexPath.row == self.indexPath.row)) {
        cell.titleColor = [UIColor colorWithHexString:@"1492ff"];
        cell.backgroundColor = [UIColor colorWithHexString:@"f5f8f7"];
    }else {
        cell.titleColor = [UIColor colorWithHexString:@"333333"];
        cell.backgroundColor = [UIColor colorWithHexString:@"ffffff"];
    }
    
    if (indexPath.row == self.cityDataArray.count - 1) {
        cell.hiddenBottomLine = YES;
    }else {
        cell.hiddenBottomLine = NO;
    }
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44.0 * APP_PARAMETER;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dataDic = self.cityDataArray[indexPath.row];
    NSArray *dataArray = dataDic[@"children"];
    NSString *titleString = dataDic[@"text"];
    NSString *idString = dataDic[@"id"];
    NSString *codeString = dataDic[@"code"];
    
    if ([self.delegate respondsToSelector:@selector(clickLeftCityViewCellWithArray:Title:ID:Code:)]) {
        [self.delegate clickLeftCityViewCellWithArray:dataArray Title:titleString ID:idString Code:codeString];
    }
    
    self.indexPath = indexPath;
}

@end
